//turn on 'loading' activity indicator
var actInd = Titanium.UI.createActivityIndicator({
    //   height: 50,
    //    width: 10
    message: "Contacting DNN site"
});
actInd.show();
// Create variable "win" to refer to current window
var win = Titanium.UI.currentWindow;

// Function loadUsers()
function loadUsers() {
    // Empty array "rowData" for our tableview
    var rowData = [];
    // Create our HTTP Client and name it "loader"
    var loader = Titanium.Network.createHTTPClient();
    // Sets the HTTP request method, and the URL to get data from
    loader.open("GET", "http://192.168.1.65/dnndev/svc/users");
    // Runs the function when the data is ready for us to process
    loader.onload = function() {


        //take the GET response and let Javascript format it for json.
        var allusers = eval('(' + this.responseText + ')');

        for (var i = 0; i < allusers.Users.length; i++) {
            var displayname = allusers.Users[i].DisplayName; // The screen name of the user
            var country = allusers.Users[i].Profile.Country; // The country of the user
            // Create a row and set its height to auto
            var row = Titanium.UI.createTableViewRow({ height: 'auto' });

            // Create the view that will contain the text and avatar
            var user_view = Titanium.UI.createView({
                height: 'auto',
                layout: 'vertical',
                top: 2,
                right: 2,
                bottom: 2,
                left: 2
            });

            // Create the label to hold the screen name
            var user_lbl = Titanium.UI.createLabel({
                text: displayname,
                font: { fontSize: 24, fontWeight: 'bold' },
                width: 'auto',
                textAlign: 'left',
                top: 0,
                left: 15,
                height: 32
            });


            // Create the label to hold the user's country
            var country_lbl = Titanium.UI.createLabel({
                text: country,
                font: { fontSize: 12, fontWeight: 'bold' },
                width: 'auto',
                textAlign: 'left',
                top: 1,
                left: 20,
                height: 20
            });

            user_view.add(user_lbl);
            user_view.add(country_lbl);
            // Add the user view to the row
            row.add(user_view);
            // Give each row a class name
            row.className = "item" + i;
            row.hasChild = "true"
            // Add row to the rowData array
            rowData[i] = row;
        }
        // Create the table view and set its data source to "rowData" array
        var tableView = Titanium.UI.createTableView({ data: rowData });
        //Add the table view to the window
        win.add(tableView);

        //hide the 'loading' graphic
        actInd.hide();

        // create table view event listener
        tableView.addEventListener('click', function(e) {
            // event data
            var index = e.index;

            // set properties on the window object, then open.  we will print them out in the new window
            var win2 = Titanium.UI.createWindow({ title: 'User Details', url: 'users_view.js' });
            win2.userData = allusers.Users[index];

            Titanium.UI.currentTab.open(win2, { animated: true });
        });
    };
    // Send the HTTP request
    loader.send();
}
loadUsers();